Skip to main content

if

Type

control structure

Summary

Evaluates a conditional expression to determine if it is true and if so executes a subsequent statement or statement list.

If the condition is false and the if control structure contains an else keyword the statement or statement list following the else keyword is executed.

The if control structure may contain one or more else if keywords which have a conditional expression. If the conditional expression for one of these evaluates to be true then the statement or statement list following that else if keyword is executed.

If the if control structure contains more than one line then the if control structure must end with an end if keyword.

Syntax

if <condition> then
<statementList>
[else
<elseStatementList>]
end if
if <condition> then
<statementList>
[else if <condition> then
<elseifStatementList>]
[else
<elseStatementList>]
end if

Description

Use the if control structure to execute a statement (or list of statements) only under certain circumstances.

The if control structure always begins with the word if. There are four forms of the if control structure:

    if condition then statementList [else elseStatementList]

This form may have a line break before the words then or else or both.

    if condition then
statementList
[else
elseStatementList]
end if

-- OR

    if condition 
then statement
[else
elseStatementList]
end if

-- OR

    if condition then
statementList
else elseStatement
end if

If the condition evaluates to true, the statement or statementList is executed; if the condition evaluates to false, the statement or statementList is skipped.

if the condition evaluates to false and the if control structure contains an else keyword, the elseStatement or elseStatementList which follows it is executed.

If the if control structure contains one or more else if keywords which have a conditional expression, and one of those conditional expressions evaluates to be true, then the respective else if statement or else if statement list following that else if keyword is executed.

If one if control structure is nested inside another, use of the second form described above is recommended, since the other forms may cause ambiguities in interpreting which else clause belongs with which if statement.

The if control structure is most suitable when you want to check a single condition. If you need to check for multiple possibilities, doing something different for each one, use a switch control structure instead.

note

The if control structure is implemented internally as a command and appears in the commandNames.

Parameters

NameTypeDescription

condition

bool

Any expression. (If the condition evaluates to true, the statements execute for that condition. If the condition evaluates to false, the statement or statement list following that condition is skipped.)

statementList

Of one or more LiveCode statements, and can also include if, switch, try, or repeat control structure|control structures.

elseIfStatementList

Of one or more LiveCode statements, and can also include if, switch, try, or repeat control structure|control structures.

elseStatementList

Of one or more LiveCode statements, and can also include if, switch, try, or repeat control structure|control structures.

Examples

if tSpeed &lt; 2 then
answer "That is slow"
else if tSpeed &lt; 5 then
answer "That is pretty fast"
else if tSpeed &lt; 10 then
answer "That is a rocket"
else
answer "You are traveling at the speed of light"
end if

control structure: repeat, switch, try

function: commandNames

glossary: statement, command, evaluate, execute, control structure

keyword: then, else, else if, end if, word

Compatibility and Support

Introduced

LiveCode 1.0

OS

mac

windows

linux

ios

android

Platforms

desktop

server

mobile